package eu.irreality.dai.ui.keyboard;

import java.awt.event.KeyEvent;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

import eu.irreality.dai.util.Observable;
import eu.irreality.dai.util.Observer;

public class RealKeyReceiver implements KeyReceiver, Observer
{

    private KeyProcessor mediator;

    private BlockingQueue<KeyEvent> keyQueue = new LinkedBlockingQueue<KeyEvent>();

    public RealKeyReceiver(KeyProcessor mediator)
    {
	this.mediator = mediator;
    }

    public void keyPressed(KeyEvent e)
    {
	keyQueue.offer(e);
    }

    // public boolean isListening(KeyEvent e)
    // {
    // return mediator.isListening(e);
    // }

    public void update(Observable o, Object arg)
    {
	if (((String) arg).equalsIgnoreCase("waiting_for_key"))
	{
	    try
	    {
		mediator.keyPressed(keyQueue.take());
	    } catch (InterruptedException ie)
	    {
		ie.printStackTrace();
	    }
	}
    }

}
